package com.zhang.gulimall.seckill.scheduled;

import com.zhang.gulimall.seckill.service.SeckillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling//开启定时任务
public class SeckillScheduled {
    @Autowired
    SeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;

    private final String upload_lock="seckill:upload:lock";

    @Scheduled(cron = "0,30 * * * * ?")
    public void upSeckillSessions(){
        System.out.println("上架秒杀商品");
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock();
        seckillService.sessionUp();
        lock.unlock();
    }
}
